﻿using System;
using System.IO.IsolatedStorage;
using System.Windows;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

using GroupChallengeWP7.GroupChallengeSvc;

namespace GroupChallengeWP7.Models
{
    public class GameState
    {
        private const string InGameKey = "InGame";
        private const string ScoreKey = "Score";

        private bool _inGame = false;
        private Game _currentGame = null;
        private Question _currentQuestion = null;
        private int _score = 0;
        private int _lastGameScore = 0;

        public event Action GameStateChanged;

        public bool InGame
        {
            get { return _inGame; }
        }

        public Game CurrentGame
        {
            get { return _currentGame; }
        }

        public Question CurrentQuestion
        {
            get { return _currentQuestion; }
        }

        public int Score
        {
            get { return _score; }
        }

        public int LastGameScore
        {
            get { return _lastGameScore; }
        }

        public void JoinGame()
        {
            _inGame = true;
            _currentGame = null;
            _currentQuestion = null;
            _score = 0;
            FireGameStateChanged();

            // Reposition the view on successful join.
            App.TheMainPage.PivotContainer.SelectedIndex = 0;
        }

        public void LeaveGame()
        {
            _lastGameScore = _score;

            _inGame = false;
            _currentGame = null;
            _currentQuestion = null;
            _score = 0;
            FireGameStateChanged();
        }

        public void UpdateGameState(Game currentGame, Question currentQuestion)
        {
            _currentGame = currentGame;
            _currentQuestion = currentQuestion;

            // If the game is over - clear my score.
            if (currentGame.CurrentQuestionId == 0)
            {
                _lastGameScore = _score;
                _score = 0;
            }

            FireGameStateChanged();
        }

        public void ScorePoint()
        {
            _score++;

            var stream = Application.GetResourceStream(new Uri("Assets/Clap.wav", UriKind.Relative)).Stream;
            SoundEffect effect = SoundEffect.FromStream(stream);
            FrameworkDispatcher.Update();
            effect.Play();

            FireGameStateChanged();
        }

        public void FireGameStateChanged()
        {
            if (GameStateChanged != null)
                GameStateChanged();
        }

        public void SaveSettings(IsolatedStorageSettings settings)
        {
            // Add the setting if not already.
            if (!settings.Contains(InGameKey))
                settings.Add(InGameKey, false);
            if (!settings.Contains(ScoreKey))
                settings.Add(ScoreKey, 0);

            settings[InGameKey] = _inGame;
            settings[ScoreKey] = _score;
        }

        public void LoadSettings(IsolatedStorageSettings settings)
        {
            settings.TryGetValue<bool>(InGameKey, out _inGame);
            settings.TryGetValue<int>(ScoreKey, out _score);
        }
    }
}
